package com.example.controller;

import com.example.entity.User;
import com.example.entity.User0;
import com.example.repository.DepartmentRepository;
import com.example.repository.EmployeeRepository;
import com.example.entity.Department;
import com.example.entity.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.Collection;
import java.util.List;

@Controller
public class EmployeeController {
    @Autowired
    EmployeeRepository employeeRepository;
    @Autowired
    DepartmentRepository departmentRepository;

    @Autowired
    User0 user0;

    @RequestMapping("/employee")
    public String list(Model model, HttpSession session){
        System.out.println(user0.getUser());
        User loginUser = (User) session.getAttribute("loginUser");
        Department dep = (Department) session.getAttribute("dep");
        Collection<Employee> employees = employeeRepository.findAllByUserId(loginUser.getId());
        model.addAttribute("emps",employees);
        return "emp/list";
    }
    @GetMapping("/emp")
    public String toAdd(Model model,HttpSession session){

        User loginUser = (User) session.getAttribute("loginUser");
        //查出所有部门信息

        Collection<Department> departments = departmentRepository.findAllByUserId(loginUser.getId());
        if(departments.size() <= 0){
            model.addAttribute("msg_dep","没有配置部门，请先配置部门");
            return "emp/list";
        }else {
            model.addAttribute("msg_dep",null);
        }
        model.addAttribute("departments",departments);
        return "emp/add";
    }

    @PostMapping("/emp")
    public String addEmp(Employee employee,HttpSession session) throws ParseException {
        User loginUser = (User) session.getAttribute("loginUser");
        List<Employee> employees = employeeRepository.findAllByUserId(loginUser.getId());
        if (employees.size() > 0){
            Employee employee1 = employees.get(employees.size() - 1);
            employee.setId(employee1.getId()+1);
        } else {
            employee.setId(1001);
        }

        employee.setUserId(loginUser.getId());
        employee.setDepartmentName(departmentRepository.findByIdAndUserId(employee.getDepartmentId(),loginUser.getId()).getName());
        //调用业务方法保存员工信息
        employeeRepository.saveAndFlush(employee);
        return "redirect:/employee";
    }

    //去员工的修改页面
    @GetMapping("/emp/{id}")
    public String toUpdateEmp(HttpSession session,Model model, @PathVariable("id")Integer id){
        User loginUser = (User) session.getAttribute("loginUser");

        Collection<Department> departments = departmentRepository.findAllByUserId(loginUser.getId());
        model.addAttribute("departments",departments);
        //查出原来的数据
        Employee employee = employeeRepository.findEmployeeByIdAndUserId(id,loginUser.getId());
        model.addAttribute("emp",employee);
        return "emp/update";
    }
    @PostMapping("/emp/{id}")
    public String updateEmp(Employee employee,HttpSession session) throws ParseException {
        User loginUser = (User) session.getAttribute("loginUser");

        employee.setUserId(loginUser.getId());//设置用户id
        //设置部门名称
        employee.setDepartmentName(departmentRepository.findByIdAndUserId(employee.getDepartmentId(),employee.getUserId()).getName());

        Employee employee1 = employeeRepository.findEmployeeByIdAndUserId(employee.getId(), loginUser.getId());
        //将主键的值赋值给新的对象
        employee.setIdd(employee1.getIdd());
        //调用方法保存员工信息
        employeeRepository.saveAndFlush(employee);
        return "redirect:/employee";
    }
    //删除员工
    @RequestMapping("/deleteEmp/{id}")
    public String deleteEmp(HttpSession session,@PathVariable("id")Integer id) throws ParseException {
        //调用业务方法保存员工信息
        User loginUser = (User) session.getAttribute("loginUser");
        employeeRepository.deleteByIdAndUserId(id,loginUser.getId());
        return "redirect:/employee";
    }
}
